//
//  WBStatusCell.swift
//  Swift-WB
//
//  Created by lxy on 2017/6/13.
//  Copyright © 2017年 lixiya. All rights reserved.
//

import UIKit

let kPictureImgVTag = 100

class WBStatusCell: TableViewCell {
    lazy var pictures: Array<UIImageView> = Array()

    lazy var avatarImgV: UIImageView = {
        var imgV = UIImageView()
        imgV.backgroundColor = UIColor.lightGray
        return imgV
    }()
    
    lazy var nameLab: UILabel = {
        var lab = UILabel(font: UIFont.systemFont(ofSize: 14), textColor: UIColor.gray, textAlignment: .left)
        return lab
    }()
    
    lazy var timeLab: UILabel = {
        var lab = UILabel(font: UIFont.systemFont(ofSize: 11), textColor: UIColor.lightGray, textAlignment: .left)
        return lab
    }()
    
    lazy var sourceLab: UILabel = {
        var lab = UILabel(font: UIFont.systemFont(ofSize: 11), textColor: UIColor.lightGray, textAlignment: .left)
        return lab
    }()
    
    lazy var contentLab: UILabel = {
        var lab = UILabel(font: UIFont.systemFont(ofSize: 14), textColor: UIColor.black, textAlignment: .left)
        lab.numberOfLines = 0
        return lab
    }()
    
    lazy var picturesView: UIView = {[unowned self] in
        var v = UIView()
        v.backgroundColor = kColor_bg
        v.isHidden = true
        
        for i in 0...8 {
            let imgV = UIImageView()
            imgV.contentMode = .scaleAspectFit
            imgV.backgroundColor = UIColor.lightGray
            imgV.tag = kPictureImgVTag + i
            imgV.clipsToBounds = true
            imgV.isUserInteractionEnabled = true
            imgV.isHidden = true
            
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
            imgV.addGestureRecognizer(tap)
            v.addSubview(imgV)
            self.pictures.append(imgV)
        }
        return v
    }()
    
    lazy var lineView: UIView = {
        var v = UIView()
        v.backgroundColor = kColor_line
        return v
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - setUpUI
    func setUpUI() {
        contentView.addSubview(nameLab)
        contentView.addSubview(avatarImgV)
        contentView.addSubview(timeLab)
        contentView.addSubview(sourceLab)
        contentView.addSubview(contentLab)
        contentView.addSubview(picturesView)
        contentView.addSubview(lineView)

        avatarImgV.snp.makeConstraints { (make) in
            make.top.left.equalTo(15)
            make.width.height.equalTo(40)
        }
        avatarImgV.setRadius(radius: 20)
        
        nameLab.snp.makeConstraints { (make) in
            make.left.equalTo(avatarImgV.snp.right).offset(15)
            make.right.equalTo(-15)
            make.top.equalTo(avatarImgV)
        }
        
        timeLab.snp.makeConstraints { (make) in
            make.left.equalTo(avatarImgV.snp.right).offset(15)
            make.top.equalTo(nameLab.snp.bottom).offset(5)
        }
        
        sourceLab.snp.makeConstraints { (make) in
            make.left.equalTo(timeLab.snp.right).offset(15)
            make.right.equalTo(-15)
            make.centerY.equalTo(timeLab)
        }
        
        contentLab.snp.makeConstraints { (make) in
            make.left.equalTo(avatarImgV.snp.right).offset(15)
            make.right.equalTo(-15)
            make.top.equalTo(avatarImgV.snp.bottom).offset(5)
        }
        
        picturesView.snp.makeConstraints ({ (make) in
            make.top.equalTo(contentLab.snp.bottom).offset(KWBStatusCellPadding)
            make.left.equalTo(contentLab)
            make.right.equalTo(-kWBStatusCellMargin)
            make.height.equalTo(0)
        })
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(contentView)
            make.height.equalTo(0.8)
        }
        
        
        
    }
    
    //MARK: - model
    var statusViewModel: WBStatusViewModel? {
        
        didSet {
            guard let statusViewModel = statusViewModel else { return }
            
            avatarImgV.xy_setImage(urlStr: statusViewModel.status?.user?.profile_image_url, placeholder: kAvatarImgPlaceH)
            nameLab.text = statusViewModel.status?.user?.screen_name
            timeLab.text = statusViewModel.status?.created_at
            sourceLab.text = statusViewModel.status?.source
            contentLab.text = statusViewModel.status?.text
            
            if statusViewModel.picturesViewHeight == 0 {
                picturesView.isHidden = true
            }else{
                picturesView.isHidden = false
                picturesView.snp.updateConstraints ({ (make) in
                    make.height.equalTo(statusViewModel.picturesViewHeight)
                })
                
                for (idx, imgV) in pictures.enumerated() {
                    if idx>=statusViewModel.pic_urls.count {
                        imgV.isHidden = true
                    }else{
                        imgV.isHidden = false
                        
                        let itemW = widthForItem()
                        let itemH = heightForItem()
                        let rowItems = numberOfItemsInRow()
                        
                        imgV.frame = CGRect(x: (itemW+KWBStatusCellPadding)*CGFloat((idx%rowItems)), y: (itemH+KWBStatusCellPadding)*CGFloat((idx/rowItems)), width: itemW, height: itemH)
                        imgV.xy_setImage(urlStr: statusViewModel.pic_urls[idx].thumbnail_pic, placeholder: nil)
                    }
                }
                
               
            }
        }

    }
    
}

//MARK: - Privates
extension WBStatusCell {
    
    // 图片宽度
    fileprivate func widthForItem() -> CGFloat {
        if statusViewModel?.pic_urls.count == 1 {
            return 100
        }else{
            return kWBStatusCellPictureWH
        }
    }
    
    // 图片高度
    fileprivate func heightForItem() -> CGFloat {
        if statusViewModel?.pic_urls.count == 1 {
            return 100
        }else{
            return kWBStatusCellPictureWH
        }
    }
    
    // 一行返回最多几张图片
    fileprivate func numberOfItemsInRow() -> Int {
        if statusViewModel?.pic_urls.count == 4 {
            return 2
        }else{
            return 3
        }
    }
    
    
}

extension WBStatusCell {

    func tapAction(recognizer: UITapGestureRecognizer) {
        
    }

}


